package com.framework.service.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.framework.service.core.dao.mapper.CityMapper;
import com.framework.service.core.dao.model.City;
import com.framework.service.core.pojo.CityDo;
import com.framework.service.core.service.ICityService;
import com.framework.core.api.request.CityQueryReq;
import com.framework.core.api.response.CityDataRes;
import com.framework.core.api.response.CityQueryRes;
import com.framework.service.core.dao.mapper.CityMapper;
import com.framework.service.core.dao.model.City;
import com.framework.service.core.pojo.CityDo;
import com.framework.service.core.service.ICityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author zhoubb
 * @version V1.0
 * @Date 2020/5/5 16:58
 * @since JDK 1.8
 */
@Slf4j
@Service
public class CityServiceImpl implements ICityService {

    @Autowired
    private CityMapper cityMapper;

    @Override
    public List<CityQueryRes> listCityByCondition(CityQueryReq cityQueryReq) {
        List<CityQueryRes> cityQueryResList = new ArrayList<>();
        if(cityQueryReq == null) {
            log.warn("根据城市类型获取城市列表-参数为空");
            return cityQueryResList;
        }
        log.info("correlationID=[{}]-根据城市类型获取城市列表-开始-请求参数=[{}]", cityQueryReq.getCorrelationID(), JSON.toJSONString(cityQueryReq));
        City city = new City();

        try {
            BeanUtils.copyProperties(cityQueryReq, city);
            List<City> cityList =  cityMapper.selectByCondition(city);
            cityList.forEach(cityTemp -> {
                CityQueryRes cityQueryRes = new CityQueryRes();
                BeanUtils.copyProperties(cityTemp, cityQueryRes);
                cityQueryResList.add(cityQueryRes);
            });
        } catch (Exception e) {
            log.error("correlationID=[{}]-根据城市类型获取城市列表-系统异常", cityQueryReq.getCorrelationID(), e);
            return null;
        }
        log.info("correlationID=[{}]-根据城市类型获取城市列表-结束-返回结果=[{}]", cityQueryReq.getCorrelationID(), JSON.toJSONString(cityQueryResList));
        return cityQueryResList;
    }


    @Override
    public List<CityDataRes> listCityAll() {
        List<CityDataRes> cityDataResList = new ArrayList<>();
        log.info("获取城市列表-开始");
        try{
            List<CityDo> cityDoList = cityMapper.selectCityAll();
            List<CityDo> cityDoListType_1 = cityDoList.stream().filter(cityDo -> 1 == cityDo.getType()).collect(Collectors.toList());
            List<CityDo> cityDoListType_2 = cityDoList.stream().filter(cityDo -> 2 == cityDo.getType()).collect(Collectors.toList());
            cityDoListType_1.forEach(cityDo -> {
                CityDataRes cityDataRes = new CityDataRes();
                BeanUtils.copyProperties(cityDo, cityDataRes);
                List<CityDataRes> cityDataResChildList = new ArrayList<>();
                cityDoListType_2.forEach(cityDo_type2 -> {
                    if(cityDo_type2.getPid().equals(Integer.valueOf(cityDo.getValue()))) {
                        CityDataRes cityDataSubRes = new CityDataRes();
                        BeanUtils.copyProperties(cityDo_type2, cityDataSubRes);
                        cityDataResChildList.add(cityDataSubRes);
                    }
                });
                cityDataRes.setChildren(cityDataResChildList);
                cityDataResList.add(cityDataRes);
            });
        }catch (Exception e) {
            log.error("获取城市列表-系统异常", e);
            return null;
        }
        log.info("获取城市列表-结束-返回结果=[{}]", JSON.toJSONString(cityDataResList));
        return cityDataResList;
    }
}
